<template>
  <div class="login-container">
    <div class="login-box">
      <div
        style="
          padding: 40px;
          background-color: white;
          border-radius: 5px;
          box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        "
      >
        <el-form
          ref="formRef"
          :rules="data.rules"
          :model="data.form"
          style="width: 400px"
        >
          <div
            style="
              margin-bottom: 30px;
              margin-left: 20px;
              font-size: 24px;
              display: flex;
              justify-content: center;
              color: #5381bf;
              font-weight: bold;
            "
          >
            欢迎登录在线购物商城
          </div>
          <el-form-item prop="username">
            <el-input
              size="large"
              v-model="data.form.username"
              placeholder="请输入账号"
              :prefix-icon="User"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              show-password
              size="large"
              v-model="data.form.password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              clearable
            />
          </el-form-item>
          <el-form-item prop="role">
            <el-select
              v-model="data.form.role"
              style="width: 100%"
              size="large"
            >
              <el-option value="BUYER" label="买家">买家</el-option>
              <el-option value="ADMIN" label="卖家">卖家</el-option>
            </el-select>
          </el-form-item>
          <div style="margin-bottom: 20px">
            <el-button
              @click="login"
              size="large"
              style="width: 100%"
              type="primary"
              >登录</el-button
            >
          </div>
          <div style="text-align: right">
            还没有账号？去<a style="color: #5381bf" href="/register">注册</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import request from "../utils/request";
import { ElMessage } from "element-plus";

const data = reactive({
  form: {
    role: "BUYER",
    username: "user1",
    password: "111111",
  },
  rules: {
    username: [{ required: true, message: "请输入账号", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  },
});

// 登录组件修改部分
const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post("/identity/login", data.form).then((res) => {
        if (res.code === "200") {
          // 登录成功
          // 将用户信息和token一起存储
          localStorage.setItem("xm-pro-token", res.data.token);
          localStorage.setItem("xm-pro-user", JSON.stringify(res.data.user));
          console.log("登录成功,token已存储");
          //debugger;
          ElMessage.success("登录成功");

          // 根据不同的角色跳转到不同的页面
          if (data.form.role === "BUYER") {
            // 买家
            setTimeout(() => {
              location.href = "/bmanager/index/products";
            }, 300);
          } else if (data.form.role === "ADMIN") {
            // 卖家
            setTimeout(() => {
              location.href = "/manager/products";
            }, 300);
          }
        } else {
          ElMessage.error(res.msg);
        }
      });
    }
  });
};

const formRef = ref();
</script>
<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/Userlogin01.jpg");
  background-size: cover;
}

.login-box {
  width: 400px;
  height: 300px;
  position: absolute;
  top: 45%;
  left: 35%;
  margin-top: -100px;
  display: flex;
  align-items: center;
}
</style>
